import 'package:flutter_bili_app/db/hi_cache.dart';
import 'package:flutter_bili_app/http/core/hi_net.dart';
import 'package:flutter_bili_app/http/request/basic_request.dart';
import 'package:flutter_bili_app/http/request/login_request.dart';
import 'package:flutter_bili_app/http/request/registration_request.dart';

class LoginDao {
  ///登录后令牌
  static const String BOARDING_PASS = "boarding-pass";
  static login(String userName, String password) {
    return _send(userName, password);
  }

  static registration(String userName, String password, String imoocId,
      String orderId, String courseFlag) {
    return _send(userName, password,
        imoocId: imoocId, orderId: orderId, courseFlag: courseFlag);
  }

  static _send(String userName, String password,
      {imoocId, orderId, courseFlag}) async {
    BaseRequest request;
    if (imoocId != null && orderId != null && courseFlag != null) {
      request = RegistrationRequest();
    } else {
      request = LoginRequest();
    }
    request
        .add("userName", userName)
        .add("password", password)
        .add("imoocId", imoocId)
        .add("orderId", orderId)
        .add("courseFlag", courseFlag);
    var result = await HiNet.getInstance().fire(request);
    if (result['code'] == 0 && result['data'] != null) {
      HiCache.getInstance().setString(BOARDING_PASS, result['data']);
      print("token储存成功");
    }
    print("获取的结果为：$result");
    return result;
  }
}
